<script setup>
import { useRouter } from 'vue-router'
import { ref, onMounted } from 'vue'

const router = useRouter()

// 响应式数据
const quickStats = ref({
  todaySales: 0,
  todayOrders: 0,
  activeMembers: 0,
  lowStockItems: 0
})

// 功能模块列表
const modules = ref([
  {
    id: 'pos',
    title: '快速收银',
    icon: '💰',
    description: '快速处理商品销售，支持扫码、手动输入',
    color: '#409eff',
    route: '/pos'
  },
  {
    id: 'inventory',
    title: '商品管理',
    icon: '📦',
    description: '管理商品信息、库存、价格和分类',
    color: '#67c23a',
    route: '/inventory'
  },
  {
    id: 'members',
    title: '会员管理',
    icon: '👥',
    description: '会员信息、积分管理、会员卡',
    color: '#e6a23c',
    route: '/members'
  },
  {
    id: 'reports',
    title: '报表统计',
    icon: '📊',
    description: '销售报表、利润分析、趋势图表',
    color: '#f56c6c',
    route: '/reports'
  },
  {
    id: 'settings',
    title: '系统设置',
    icon: '⚙️',
    description: '收银机配置、打印设置、权限管理',
    color: '#909399',
    route: '/settings'
  },
  {
    id: 'backup',
    title: '数据备份',
    icon: '💾',
    description: '数据备份恢复、系统维护',
    color: '#9c27b0',
    route: '/backup'
  }
])

// 最近交易记录
const recentTransactions = ref([
  {
    id: 1,
    time: '14:30:25',
    amount: 128.50,
    items: 3,
    type: '现金'
  },
  {
    id: 2,
    time: '14:25:18',
    amount: 89.00,
    items: 2,
    type: '微信'
  },
  {
    id: 3,
    time: '14:20:45',
    amount: 256.80,
    items: 5,
    type: '支付宝'
  }
])

// 导航到指定模块
const navigateToModule = (route) => {
  router.push(route)
}

// 开始快速收银
const startQuickSale = () => {
  router.push('/pos')
}

// 组件挂载时初始化数据
onMounted(() => {
  // 模拟数据加载
  quickStats.value = {
    todaySales: 2580.50,
    todayOrders: 45,
    activeMembers: 128,
    lowStockItems: 12
  }
})
</script>

<template>
  <div class="pos-home">
    <!-- 快速统计 -->
    <div class="quick-stats">
      <div class="stat-card">
        <div class="stat-icon">💰</div>
        <div class="stat-content">
          <div class="stat-value">¥{{ quickStats.todaySales.toFixed(2) }}</div>
          <div class="stat-label">今日销售额</div>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon">📋</div>
        <div class="stat-content">
          <div class="stat-value">{{ quickStats.todayOrders }}</div>
          <div class="stat-label">今日订单</div>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon">👥</div>
        <div class="stat-content">
          <div class="stat-value">{{ quickStats.activeMembers }}</div>
          <div class="stat-label">活跃会员</div>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon">⚠️</div>
        <div class="stat-content">
          <div class="stat-value">{{ quickStats.lowStockItems }}</div>
          <div class="stat-label">库存预警</div>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 快速收银按钮 -->
      <div class="quick-sale-section">
        <button class="quick-sale-btn" @click="startQuickSale">
          <div class="btn-icon">💰</div>
          <div class="btn-text">
            <div class="btn-title">开始收银</div>
            <div class="btn-subtitle">快速处理商品销售</div>
          </div>
        </button>
      </div>

      <!-- 功能模块网格 -->
      <div class="modules-grid">
        <div 
          v-for="module in modules" 
          :key="module.id"
          class="module-card"
          @click="navigateToModule(module.route)"
        >
          <div class="module-icon" :style="{ backgroundColor: module.color }">
            {{ module.icon }}
          </div>
          <div class="module-content">
            <h3 class="module-title">{{ module.title }}</h3>
            <p class="module-description">{{ module.description }}</p>
          </div>
        </div>
      </div>

      <!-- 最近交易记录 -->
      <div class="recent-transactions">
        <h3 class="section-title">最近交易</h3>
        <div class="transactions-list">
          <div 
            v-for="transaction in recentTransactions" 
            :key="transaction.id"
            class="transaction-item"
          >
            <div class="transaction-time">{{ transaction.time }}</div>
            <div class="transaction-info">
              <div class="transaction-amount">¥{{ transaction.amount.toFixed(2) }}</div>
              <div class="transaction-details">
                {{ transaction.items }}件商品 · {{ transaction.type }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.pos-home {
  min-height: 100%;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 12px;
}

/* 快速统计 */
.quick-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.stat-card {
  background: white;
  padding: 12px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
}

.stat-icon {
  font-size: 24px;
  margin-right: 10px;
}

.stat-value {
  font-size: 18px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 2px;
}

.stat-label {
  font-size: 10px;
  color: #606266;
}

/* 主要内容区域 */
.main-content {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 20px;
}

/* 快速收银区域 */
.quick-sale-section {
  grid-column: 1;
}

.quick-sale-btn {
  width: 100%;
  background: linear-gradient(135deg, #409eff, #67c23a);
  border: none;
  border-radius: 12px;
  padding: 24px 20px;
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 6px 20px rgba(64, 158, 255, 0.3);
}

.quick-sale-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 24px rgba(64, 158, 255, 0.4);
}

.btn-icon {
  font-size: 32px;
  margin-right: 12px;
}

.btn-title {
  font-size: 16px;
  font-weight: 700;
  color: white;
  margin-bottom: 4px;
}

.btn-subtitle {
  font-size: 10px;
  color: rgba(255, 255, 255, 0.8);
}

/* 功能模块网格 */
.modules-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.module-card {
  background: white;
  border-radius: 8px;
  padding: 16px;
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #f0f0f0;
}

.module-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

.module-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  margin-right: 10px;
  color: white;
}

.module-title {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 4px 0;
}

.module-description {
  font-size: 10px;
  color: #606266;
  margin: 0;
  line-height: 1.3;
}

/* 最近交易记录 */
.recent-transactions {
  background: white;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 12px 0;
}

.transactions-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.transaction-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
}

.transaction-item:last-child {
  border-bottom: none;
}

.transaction-time {
  font-size: 10px;
  color: #909399;
}

.transaction-amount {
  font-size: 12px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 1px;
}

.transaction-details {
  font-size: 8px;
  color: #606266;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .main-content {
    grid-template-columns: 1fr;
  }
  
  .quick-sale-section {
    grid-column: 1;
  }
}

@media (max-width: 768px) {
  .pos-home {
    padding: 8px;
  }
  
  .quick-stats {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }
  
  .modules-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  
  .quick-sale-btn {
    padding: 20px 16px;
  }
  
  .btn-icon {
    font-size: 28px;
  }
  
  .btn-title {
    font-size: 14px;
  }
  
  .stat-card {
    padding: 10px;
  }
  
  .module-card {
    padding: 12px;
  }
  
  .recent-transactions {
    padding: 12px;
  }
}

@media (max-width: 480px) {
  .quick-stats {
    grid-template-columns: 1fr;
  }
  
  .stat-card {
    padding: 8px;
  }
  
  .module-card {
    padding: 10px;
  }
  
  .quick-sale-btn {
    padding: 16px 12px;
  }
  
  .btn-icon {
    font-size: 24px;
    margin-right: 8px;
  }
  
  .btn-title {
    font-size: 12px;
  }
  
  .btn-subtitle {
    font-size: 8px;
  }
  
  .module-icon {
    width: 32px;
    height: 32px;
    font-size: 14px;
  }
  
  .module-title {
    font-size: 12px;
  }
  
  .module-description {
    font-size: 8px;
  }
}
</style>
